import { Button, Select, InputNumber, Input, message, TreeSelect, Switch } from 'antd';
import { getColorTextLoadMap, onMouseOutBtn, removeDateHmsStr, renderTableText, tableSorter, tooltipLength, priceFormat } from '@/utils';
import { PlusCircleOutlined, DeleteOutlined, AppstoreAddOutlined } from '@ant-design/icons';
import { getLoadMapList, getSelectDataMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { findObjectById } from '@/utils/findObjectById';

/** 材料表格 */
const openMaterielColumns = (
  addMaterialModalStatus,
  assetTypeList,
  midiTypeList,
  editTable,
  openMaterialModalStatus,
  delSelectRow,
  onEditTable,
  pageType,
  isHaveContract,
) => {
  const isDetail: boolean = pageType == 'detail'; // 是否是查看
  const isEdit: boolean = pageType == 'edit'; // 是否是查看

  let tableConfig: Record<string, any>[] = [
    {
      title: '物资名称',
      dataIndex: 'assetName',
      key: 'assetName',
      fixed: 'left',
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string, record: any, index: number) => {
        return record.addRow == 1 || isEdit ? (
          <Input
            value={text}
            onChange={(e) => {
              editTable({ type: 'assetName', value: e.target.value, index, addType: 1 });
            }}
            disabled={isDetail}
          />
        ) : (
          renderTableText(text, true)
        );
      },
    },
    {
      title: '物资分类',
      dataIndex: 'assetClass',
      key: 'assetClass',
      visible: true,
      width: 200,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetClass'),
      render: (text: string, record: any, index: number) => {
        return (
          <TreeSelect
            value={text}
            style={{ width: '100%' }}
            treeData={assetTypeList}
            fieldNames={{
              label: 'name',
              value: 'id',
              children: 'children',
            }}
            placeholder="请选择"
            disabled={isDetail}
            showSearch={true}
            treeNodeFilterProp={'name'}
            allowClear={true}
            onChange={(e) => {
              if (e) {
                editTable({ type: 'assetClass', value: e, index, addType: 1 });
              } else {
                editTable({ type: 'assetClass', value: '', index, addType: 1 });
              }
            }}
          />
        );
      },
    },
    {
      title: '品牌',
      dataIndex: 'brandType',
      key: 'brandType',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'brandType'),
      render: (text: string, record: any, index: number) => {
        return record.addRow == 1 || isEdit ? (
          <Input
            value={text}
            onChange={(e) => {
              editTable({ type: 'brandType', value: e.target.value, index, addType: 1 });
            }}
            disabled={isDetail}
          />
        ) : (
          renderTableText(text, true)
        );
      },
    },
    // {
    //   title: '物料编码',
    //   dataIndex: 'AssetCode',
    //   key: 'AssetCode',
    //   fixed: 'left',
    //   width: 100,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'assetCode'),
    //   render: (text: string, record: any, index: number) => {
    //     return (
    //       <Input
    //         value={text}
    //         onChange={(e) => {
    //           onEditTable({ type: 'assetCode', value: e.target.value, record, index });
    //         }}
    //         disabled={isDetail}
    //       />
    //     )
    //   },
    // },
    {
      title: '规格型号',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'paramsInfo'),
      render: (text: string, record: any, index: number) => {
        return record.addRow == 1 || isEdit ? (
          <Input
            value={text}
            onChange={(e) => {
              editTable({ type: 'paramsInfo', value: e.target.value, index, addType: 1 });
            }}
            disabled={isDetail}
          />
        ) : (
          renderTableText(text, true)
        );
      },
    },
    {
      title: '数量',
      dataIndex: 'expectNum',
      key: 'expectNum',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'expectNum'),
      render: (text: string) => {
        return <span>{text || '--'}</span>;
        // renderTableText(text, true)
      },
    },
    {
      title: '量化单位',
      dataIndex: 'unitOfMeasurement',
      key: 'unitOfMeasurement',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitOfMeasurement'),
      render: (text: string, record: any, index: number) => {
        return (
          <Select
            value={text}
            style={{ width: '100%' }}
            options={midiTypeList}
            fieldNames={{
              label: 'name',
              value: 'value',
            }}
            placeholder="请选择"
            disabled={isDetail}
            onSelect={(e) => {
              editTable({ type: 'unitOfMeasurement', value: e, index, addType: 1 });
            }}
          />
        );
      },
    },
    {
      title: '单价',
      dataIndex: 'assetValue',
      key: 'assetValue',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetValue'),
      render: (text: number | any, record: any, index: number) => {
        return record.addRow == 1 || isEdit ? (
          <InputNumber
            value={text}
            onChange={(e: any) => {
              editTable({ type: 'assetValue', value: e, index, addType: 1 });
            }}
            precision={2}
          />
        ) : (
          <span>{text ? priceFormat(text) : `0.00`}</span>
        );
      },
    },
    {
      title: '总价',
      dataIndex: 'totalValue',
      key: 'totalValue',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'totalValue'),
      render: (text: number | any, record: any, index: number) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '到货数量',
      dataIndex: 'quantityOrArea',
      key: 'quantityOrArea',
      // fixed: 'right',
      visible: true,
      width: 110,
      sorter: (a: any, b: any) => tableSorter(a, b, 'quantityOrArea'),
      render: (value: number | any, record, index) => {
        return (
          <InputNumber
            value={value}
            onChange={(e) => {
              editTable({ type: 'quantityOrArea', value: e, index, addType: 1 });
              if (e > record.expectNum) {
                message.warning('输入数量超过可入库数量，请重新输入！');
                return;
              }
              onEditTable({ type: 'quantityOrArea', value: e, index, record });
            }}
            disabled={isDetail}
            precision={2}
          />
        );
      },
    },
    {
      title: '外观是否完好',
      dataIndex: 'waiguan',
      key: 'waiguan',
      // fixed: 'right',
      visible: true,
      width: 140,
      sorter: (a: any, b: any) => tableSorter(a, b, 'waiguan'),
      render: (value: number | any, record, index) => {
        return (
          <Switch
            checkedChildren="是"
            unCheckedChildren="否"
            onChange={(value) => {
              onEditTable({ type: 'waiguan', value, index, record });
            }}
            disabled={isDetail}
            checked={Boolean(value)}
          />
          // <Select
          //   value={value}
          //   onChange={(e) => {
          //     onEditTable({ type: 'waiguan', value: e, index, record });
          //   }}
          //   disabled={isDetail}
          //   style={{ width: '100%' }}
          //   options={[
          //     {
          //       value: 1,
          //       label: '是',
          //     },
          //     {
          //       value: 0,
          //       label: '否',
          //     },
          //   ]}
          // />
        );
      },
    },
    {
      title: '技术参数是否一致',
      dataIndex: 'jishu',
      key: 'jishu',
      // fixed: 'right',
      visible: true,
      width: 164,
      sorter: (a: any, b: any) => tableSorter(a, b, 'jishu'),
      render: (value: number | any, record, index) => {
        return (
          <Switch
            checkedChildren="是"
            unCheckedChildren="否"
            onChange={(value) => {
              onEditTable({ type: 'jishu', value, index, record });
            }}
            disabled={isDetail}
            checked={Boolean(value)}
          />
          // <Select
          //   value={value}
          //   onChange={(e) => {
          //     onEditTable({ type: 'jishu', value: e, index, record });
          //   }}
          //   disabled={isDetail}
          //   style={{ width: '100%' }}
          //   options={[
          //     {
          //       value: 1,
          //       label: '是',
          //     },
          //     {
          //       value: 0,
          //       label: '否',
          //     },
          //   ]}
          // />
        );
      },
    },
  ];
  if (pageType != 'detail') {
    tableConfig?.push({
      title: (
        <div style={{ width: '100%', display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
          <Button
            type="link"
            size="small"
            icon={<AppstoreAddOutlined />}
            className="manageBtntTxtStyle"
            onMouseOut={onMouseOutBtn}
            onClick={openMaterialModalStatus}
          >
            选择
          </Button>
          {!isHaveContract ? (
            <Button
              type="link"
              size="small"
              icon={<PlusCircleOutlined />}
              className="manageBtntTxtStyle"
              onClick={() => addMaterialModalStatus(1)}
              onMouseOut={onMouseOutBtn}
            >
              添加行
            </Button>
          ) : null}
        </div>
      ),
      dataIndex: 'otherBtn',
      key: 'otherBtn',
      visible: true,
      width: 120,
      fixed: 'right',
      render: (_text: any, record: any) => {
        return (
          <div className="counterpoartDelBtnStyle">
            <Button
              type="link"
              size="small"
              icon={<DeleteOutlined />}
              className={'manageBtntTxtStyle'}
              onMouseOut={onMouseOutBtn}
              onClick={() => delSelectRow(record)}
            >
              删除
            </Button>
          </div>
        );
      },
    });
  }
  return tableConfig;
};

/** 表格配置 */
const materialTableConfig = (assetTypeList) => {
  return [
    {
      title: '物料名称',
      dataIndex: 'assetName',
      key: 'assetName',
      fixed: 'left',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetName'),
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '物料编码',
    //   dataIndex: 'assetCode',
    //   key: 'assetCode',
    //   fixed: 'left',
    //   width: 100,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'assetCode'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    {
      title: '规格型号',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'paramsInfo'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '量化单位',
      dataIndex: 'unitOfMeasurementName',
      key: 'unitOfMeasurementName',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitOfMeasurementName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '数量',
      dataIndex: 'expectNum',
      key: 'expectNum',
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'expectNum'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单价',
      dataIndex: 'assetValue',
      key: 'assetValue',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetValue'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '总价',
      dataIndex: 'assetTotal',
      key: 'assetTotal',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'assetTotal'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '品牌',
      dataIndex: 'brandType',
      key: 'brandType',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'brandType'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '来源',
      dataIndex: 'projectName',
      key: 'projectName',
      visible: true,
      width: 100,
      sorter: (a: any, b: any) => tableSorter(a, b, 'projectName'),
      render: (text: string) => renderTableText(text, true),
    },
  ];
};

export { materialTableConfig, openMaterielColumns };
